package cn.njyd.corejava.thread.myday03;

class Res{
    private boolean flag = true;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public boolean getFlag() {
        return this.flag;
    }
}

class PrintNumberThread implements Runnable{
    private Res res;

    public PrintNumberThread(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        for (int i=1;i<=51;i+=2) {
            synchronized (this.res) {
                if (!res.isFlag()) {
                    try {
                        this.res.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                System.out.print(i+""+(i+1));
                this.res.setFlag(!this.res.isFlag());
                this.res.notify();
            }
        }
    }
}

class PrintABCThread implements Runnable{
    private Res res;

    public PrintABCThread(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        for (int i=1;i<=26;i++) {
            synchronized (this.res) {
                if (res.isFlag()){
                    try {
                        this.res.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print((char)(64+i));
                this.res.setFlag(!this.res.isFlag());
                this.res.notify();
            }
        }
    }
}
public class ThreadWaitNotifyDemo01 {
    public static void main(String[] args) {
        Res res = new Res();
        new Thread(new PrintNumberThread(res)).start();
        new Thread(new PrintABCThread(res)).start();

    }
}
